import { PlayerGroup } from "@sapi-game/gamePlayer/playerGroup";
import { Game } from "@sapi-game/main";
import { elevatorCatGame } from "@/elevatorCat/game";
import { elevatorCatModule } from "@/elevatorCat/module";
import { ElevatorCatPlayer } from "@/elevatorCat/player";

export interface eleEndConfigs {
    winTeam: PlayerGroup<ElevatorCatPlayer>;
    subtitle: string;
    msg?: string;
}

export class EleEndState extends elevatorCatModule.State<eleEndConfigs> {
    override onEnter(): void {
        if (this.config) {
            this.showWin(
                this.config.winTeam,
                this.config.subtitle,
                this.config.msg
            );
        }
        this.context.group.runCommand("effect @s clear");
    }

    showWin(
        winTeam: PlayerGroup<ElevatorCatPlayer>,
        subtitle: string,
        msg?: string
    ) {
        this.context.group.runCommand("title @s subtitle " + subtitle);
        this.context.group.runCommand("title @s title 游戏结束");
        if (msg) this.context.group.sendMessage(msg);
        //生成烟花
        this.runner.run(async (r) => {
            for (let i = 0; i < 5; i++) {
                r.do(() =>
                    winTeam.forEach((p) =>
                        p.player?.dimension.spawnEntity(
                            "minecraft:fireworks_rocket",
                            p.player.location
                        )
                    )
                );
                await r.wait(10);
            }
            await r.wait(20);
            r.do(() => Game.manager.stopGame(elevatorCatGame));
        });
    }
}
